package com.ruoyi.web.controller.datastatistics;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.datastatistics.domain.TerminalsFreezeXb;
import com.ruoyi.datastatistics.service.ITerminalsFreezeXbService;
import com.ruoyi.datastatistics.vo.TerminalUsageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Comparator;
import java.util.List;

/**
 * 小冻结数据（设备维度）Controller
 *
 * @author ruoyi
 * @date 2023-09-14
 */
@RestController
@RequestMapping("/datastatistics/terminalsfreezexb")
public class TerminalsFreezeXbController extends BaseController {
    @Autowired
    private ITerminalsFreezeXbService terminalsFreezeXbService;

    /**
     * 查询小冻结数据（设备维度）列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:list')")
    @GetMapping("/list")
    public TableDataInfo list(TerminalsFreezeXb terminalsFreezeXb) {
        startPage();
        List<TerminalsFreezeXb> list = terminalsFreezeXbService.selectTerminalsFreezeXbList(terminalsFreezeXb);
        return getDataTable(list);
    }

    /**
     * 查询小冻结数据（设备维度）列表
     */
    @PreAuthorize("@ss.hasAnyPermi('datastatistics:terminalsfreezexb:list,basedata:terminalxb:list')")
    @GetMapping("/listThirtyDaysFlow")
    public AjaxResult listThirtyDaysFlow(TerminalsFreezeXb terminalsFreezeXb) {
        List<TerminalsFreezeXb> list = terminalsFreezeXbService.listThirtyDaysFlow(terminalsFreezeXb);
        if (CollUtil.isNotEmpty(list)) {
            list.sort(Comparator.comparing(TerminalsFreezeXb::getFreezeTime));
        }
        return success(list);
    }

    /**
     * 导出小冻结数据（设备维度）列表
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:export')")
    @Log(title = "小冻结数据（设备维度）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TerminalsFreezeXb terminalsFreezeXb) {
        List<TerminalsFreezeXb> list = terminalsFreezeXbService.selectTerminalsFreezeXbList(terminalsFreezeXb);
        ExcelUtil<TerminalsFreezeXb> util = new ExcelUtil<TerminalsFreezeXb>(TerminalsFreezeXb.class);
        util.exportExcel(response, list, "小冻结数据（设备维度）数据");
    }

    /**
     * 获取小冻结数据（设备维度）详细信息
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(terminalsFreezeXbService.selectTerminalsFreezeXbByID(ID));
    }

    /**
     * 新增小冻结数据（设备维度）
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:add')")
    @Log(title = "小冻结数据（设备维度）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TerminalsFreezeXb terminalsFreezeXb) {
        return toAjax(terminalsFreezeXbService.insertTerminalsFreezeXb(terminalsFreezeXb));
    }

    /**
     * 修改小冻结数据（设备维度）
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:edit')")
    @Log(title = "小冻结数据（设备维度）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TerminalsFreezeXb terminalsFreezeXb) {
        return toAjax(terminalsFreezeXbService.updateTerminalsFreezeXb(terminalsFreezeXb));
    }

    /**
     * 删除小冻结数据（设备维度）
     */
    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:remove')")
    @Log(title = "小冻结数据（设备维度）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(terminalsFreezeXbService.deleteTerminalsFreezeXbByIDs(IDs));
    }

    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:listUsage')")
    @GetMapping("/getTerminalSheetsUsage")
    public TableDataInfo getTerminalSheetsUsage(TerminalUsageVO usage){
        startPage();
        return getDataTable(terminalsFreezeXbService.getUsageBySheetsIdAndDate(usage));
    }


    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:exportUsage')")
    @PostMapping("/exportTerminalSheetsUsage")
    public void exportTerminalSheetsUsage(TerminalUsageVO usage, HttpServletResponse response){
        List<TerminalUsageVO> list = terminalsFreezeXbService.getUsageBySheetsIdAndDate(usage);
        ExcelUtil<TerminalUsageVO> util = new ExcelUtil<>(TerminalUsageVO.class);
        util.exportExcel(response,list,"户表区域日用量");
    }

    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:listUsage')")
    @GetMapping("/getSheetsUsage")
    public TableDataInfo getSheetsUsage(TerminalUsageVO usage){
        SysUser user = getLoginUser().getUser();
        if (Constants.SYS_FLAG_ADMIN != user.getSystemFlag()) {
            usage.setSysUserId(user.getUserId());
        }
        if (Constants.SYS_FLAG_NORMAL_USER == user.getSystemFlag()) {
            usage.setTenantId(user.getTenantId());
        }
        startPage();
        return getDataTable(terminalsFreezeXbService.getUsageBySheetsIdAndDateWithoutTerminal(usage));
    }

    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:listUsage')")
    @GetMapping("/getSheetsTotalUsage")
    public TableDataInfo getSheetsTotalUsage(TerminalUsageVO usage){
        SysUser user = getLoginUser().getUser();
        if (Constants.SYS_FLAG_ADMIN != user.getSystemFlag()) {
            usage.setSysUserId(user.getUserId());
        }
        if (Constants.SYS_FLAG_NORMAL_USER == user.getSystemFlag()) {
            usage.setTenantId(user.getTenantId());
        }
        startPage();
        return getDataTable(terminalsFreezeXbService.getUsageBySheetsIdWithoutTerminal(usage));
    }

    @PreAuthorize("@ss.hasPermi('datastatistics:terminalsfreezexb:exportUsage')")
    @PostMapping("/exportSheetsUsage")
    public void exportSheetsUsage(TerminalUsageVO usage, HttpServletResponse response) {
        startOrderBy();
        List<TerminalUsageVO> list = terminalsFreezeXbService.getUsageBySheetsIdAndDateWithoutTerminal(usage);
        ExcelUtil<TerminalUsageVO> util = new ExcelUtil<>(TerminalUsageVO.class);
        util.hideColumn("userId", "userName", "installAddress", "terminalId");
        util.exportExcel(response, list, "户表区域日用量");
    }
}
